package team.fourth.combat.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import team.fourth.combat.commen.entity.PageResult;
import team.fourth.combat.commen.entity.QueryPageBean;
import team.fourth.combat.commen.exceptions.CheckItemException;
import team.fourth.combat.dao.CheckItemMapper;
import team.fourth.combat.pojo.CheckItem;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    CheckItemMapper checkItemMapper;

    @Override
    public void add(CheckItem checkItem) {
        checkItemMapper.add(checkItem);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //使用分页插件
//      Page page =  PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
//      checkItemMapper.findPage(queryPageBean.getQueryString());
//      return new PageResult(page.getTotal(),page.getResult());


        Page page =  PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<CheckItem> checkItems = checkItemMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),checkItems);


//
//        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
//        Page<CheckItem> page = checkItemMapper.findPage(queryPageBean.getQueryString());
//        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void delete(Integer id) {
        //根据id查询当前检查项是否存在引用
       Integer count =  checkItemMapper.findCountById(id);
        if(count > 0){
            //如果存在提示用户不能删除（可以抛异常）
            throw new CheckItemException("存在引用，不能删除");
        } else {
            //提供不存在引用就删除
            checkItemMapper.delete(id);
        }


    }

    @Override
    public CheckItem findById(Integer id) {
        return checkItemMapper.findById(id);
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemMapper.edit(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {
        return checkItemMapper.findAll();
    }
}
